﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using System.Linq.Expressions;
using System;

namespace DFGK_WMS.Repository
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        public BaseRepository(DFGK_DB context, IMapper mapper)
        {
            Context = context;
            Mapper = mapper;
        }

        public DFGK_DB Context { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Add(T obj)
        {
            try
            {
                Context.Set<T>().Add(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除：主键int类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Del(int id)
        {
            try
            {
                var obj = Context.Set<T>().Find(id);
                Context.Set<T>().Remove(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除：主键long类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Dels(long id)
        {
            try
            {
                var obj = Context.Set<T>().Find(id);
                Context.Set<T>().Remove(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 分页—显示—查询
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> GetAll()
        {
            return Context.Set<T>().AsQueryable();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Upd(T obj)
        {
            try
            {
                Context.Set<T>().Update(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        ///  获取一条记录行数
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="keySelector">排序字段</param>
        /// <param name="isAsc">排序方式</param>
        /// <returns></returns>
        public T? GetFirst(Expression<Func<T, bool>> predicate = null, Expression<Func<T, object>> keySelector = null, bool isAsc = true)
        {
            IQueryable<T> queryble = Context.Set<T>();
            if (predicate != null)
            {
                queryble = queryble.Where(predicate);
            }

            if (keySelector != null)
            {
                if (isAsc)
                {
                    queryble = queryble.OrderBy(keySelector);
                }
                else
                {
                    queryble = queryble.OrderByDescending(keySelector);
                }
             
            }

            return queryble.FirstOrDefault();
        }
    }
}